#!/usr/bin/perl -w

#=======================================================================
# wlan-list
# File ID: 4e1b3802-f744-11dd-92f4-000475e441b9
# Latskap.
#
# Character set: UTF-8
# ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of 
# file for legal stuff.
#=======================================================================

use strict;
use Getopt::Long;

$| = 1;

our $Debug = 0;

our %Opt = (

    'debug' => 0,
    'encrypted' => 0,
    'help' => 0,
    'verbose' => 0,
    'version' => 0,
    'xml' => 0,

);

our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = "0.00";

Getopt::Long::Configure("bundling");
GetOptions(

    "debug" => \$Opt{'debug'},
    "encrypted|e" => \$Opt{'encrypted'},
    "help|h" => \$Opt{'help'},
    "verbose|v+" => \$Opt{'verbose'},
    "version" => \$Opt{'version'},
    "xml|x" => \$Opt{'xml'},

) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
    print_version();
    exit(0);
}

if ($Opt{'verbose'}) {
    system("iwlist ath0 scan | less");
} elsif ($Opt{'encrypted'}) {
    system("iwlist ath0 scan | egrep '(Address:|ESSID:|Quality=|Encryption)' | perl -pe 's/(Encryption key:off)/\x1B[1m$1\x1B[m/g;'");
} elsif ($Opt{'xml'}) {
    my $found_cell = 0;
    if (open(PipeFP, "iwlist ath0 scan |")) {
        print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<wlan>\n");
        my $curr_time = time();
        my ($Sec, $Min, $Hour, $Day, $Mon, $Year, $Wday, $Yday) = gmtime($curr_time);
        $Sec = sprintf("%02u", $Sec);
        $Min = sprintf("%02u", $Min);
        $Hour = sprintf("%02u", $Hour);
        $Day = sprintf("%02u", $Day);
        $Mon = sprintf("%02u", $Mon + 1);
        $Year = sprintf("%04u", $Year + 1900);

        my $Date = "$Year-$Mon-${Day}T$Hour:$Min:${Sec}Z";
        my $end_cell = "  </cell>\n";
        my $First = 1;
        while (<PipeFP>) {
            chomp(my $Line = $_);
            D("\$Line = '$Line'");
            $Line =~ s/^ +//;
            $Line =~ s/^(\S+)\s+Scan completed :/sprintf("  <device>%s<\/device>\n  <date>$Date<\/date>", txt_to_xml($1))/e;
            if (
                $Line =~ s{^Cell (\d+) - Address: (\S+)$}
                {
                    my $Tmp = $First ? "" : $end_cell;
                    $Tmp .= sprintf(
                        "  <cell num=\"%s\">\n    <macaddr>%s<\/macaddr>",
                        txt_to_xml($1), txt_to_xml($2)
                    );
                    $Tmp;
                }e
            ) {
                $found_cell = 1;
                if ($First) {
                    $First = 0;
                }
            }
            $Line =~ s/^ESSID:"(.*)"$/sprintf("    <essid>$1<\/essid>")/e;
            $Line =~ s/^Mode:(.*)$/sprintf("    <mode>$1<\/mode>")/e;
            $Line =~ s{^
                Frequency:(\S+)\ (\S+)\ \(Channel\ (\d+)\)$
            }{
                sprintf(
                    "    <freq unit=\"%s\">%s<\/freq>\n    <channel>%s<\/channel>",
                    txt_to_xml($2), txt_to_xml($1), txt_to_xml($3)
                )
            }ex;
            $Line =~ s{^
                Quality=(\S+)\s+
                Signal\ level=(\S+)\ (\S+)\s+
                Noise\ level=(\S+)\ (\S+)
                $
            }{
                sprintf(
                    "    <quality>%s</quality>\n    <signallevel unit=\"%s\">%s</signallevel>\n    <noiselevel unit=\"%s\">%s</noiselevel>",
                    txt_to_xml($1), txt_to_xml($3), txt_to_xml($2), txt_to_xml($5), txt_to_xml($4)
                )
            }ex;
            $Line =~ s/Encryption key:(on|off)$/    <encryption>$1<\/encryption>/;
            $Line =~ s/^Bit Rate:(\S+) (\S+)$/sprintf("    <rate unit=\"%s\">%s<\/rate>", txt_to_xml($2), txt_to_xml($1))/e;
            $Line =~ s/^Extra:([a-z_]+)=(.*)$/sprintf("    <extra name=\"%s\">%s<\/extra>", txt_to_xml($1), txt_to_xml($2))/e;
            length($Line) && print("$Line\n");
        }
        close(PipeFP);
        $found_cell && print($end_cell);
    } else {
        warn("$progname: Unable to open pipe: $!");
    }
    print("</wlan>\n");
} else {
    system("wlanconfig ath0 list scan");
}

sub print_version {
    # Print program version {{{
    print("$progname v$VERSION\n");
    # }}}
} # print_version()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    if ($Opt{'verbose'}) {
        print("\n");
        print_version();
    }
    print(<<END);

List available wireless networks.

Usage: $progname [options] [file [files [...]]]

Options:

  -e, --encrypted
    List networks on a more terse format with encryption info.
  -h, --help
    Show this help.
  -v, --verbose
    Show verbose info about the networks. Can be repeated to increase 
    verbosity level.
  -x, --xml
    Create XML output.
  --version
    Print version information.
  --debug
    Print debugging messages.

END
    exit($Retval);
    # }}}
} # usage()

sub msg {
    # Print a status message to stderr based on verbosity level {{{
    my ($verbose_level, $Txt) = @_;

    if ($Opt{'verbose'} >= $verbose_level) {
        print(STDERR "$progname: $Txt\n");
    }
    # }}}
} # msg()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    print(STDERR "$File:$call_info[2] $$ $Txt\n");
    return("");
    # }}}
} # D()

sub txt_to_xml {
    # Convert plain text to XML {{{
    my $Txt = shift;
    $Txt =~ s/&/&amp;/gs;
    $Txt =~ s/</&lt;/gs;
    $Txt =~ s/>/&gt;/gs;
    $Txt =~ s/"/&quot;/gs;
    $Txt =~ s/"/&apos;/gs;
    return($Txt);
    # }}}
}

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME



=head1 SYNOPSIS

 [options] [file [files [...]]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<-v>, B<--verbose>

Increase level of verbosity. Can be repeated.

=item B<--version>

Print version information.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation, either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.
If not, see L<http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
